package com.ce.util;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ce.cases.source.dao.po.CaseSource;
import com.ce.cases.source.dao.po.CaseSourceFilesTemplate;
import com.ce.framework.context.FrameworkContext;
import com.ce.framework.datamaintain.logic.IBaseLogic;
import com.ce.framework.datamaintain.logic.ISQLMapper;
import com.ce.framework.exception.AppException;
import com.ce.framework.util.DateUtil;
import com.ce.framework.util.GeneratorUUID;
import com.ce.framework.util.StringUtil;
import com.ce.sys.login.model.User;
import com.ce.workflow.work.dao.po.WorkflowFlow;
import com.ce.workflow.work.dao.po.WorkflowMan;
import com.ce.workflow.work.dao.po.WorkflowWork;

public class FlowWorkUtil {


	private static IBaseLogic getBaseLogic() {
		IBaseLogic baseLogic=null;
		if(baseLogic==null)
			try {
				baseLogic = (IBaseLogic)FrameworkContext.getBean("baseLogic");
			} catch (AppException e) {
				e.printStackTrace();
			}
		return  baseLogic;
	}
	//案源设为已选时，即关联流程表，并把案源设为待启动。
	public static void setFlowWork1(String vcTableName,String vcRecordId,String vcType,String vcOrgId,String vcOrgName,String vcPersonId,String vcPersonName,String vcHandleOrgId,String vcHandleOrgName,String vcHandlePersonId,String vcHandlePersonName,String vcDesc){
		WorkflowWork work=new WorkflowWork();
		work.setVcId(GeneratorUUID.generateUUID());
		work.setVcRecordId(vcRecordId);
		work.setDtDate(new Date());
		work.setVcStatus("0");//已下发，待启动
		work.setVcType(vcType);
		work.setVcTableName(vcTableName);		
		
		work.setVcOrgId(vcOrgId);
		work.setVcOrgName(vcOrgName);
		work.setVcPersonId(vcPersonId);
		work.setVcPersonName(vcPersonName);
		
		work.setVcHandlePersonId(vcHandlePersonId);
		work.setVcHandlePersonName(vcHandlePersonName);
		work.setVcHandleOrgid(vcHandleOrgId);
		work.setVcHandleOrgname(vcHandleOrgName);
		
		work.setVcDelFlag("0");
		work.setVcDesc(vcDesc);
		
		WorkflowFlow flow=new WorkflowFlow();
		
		flow.setVcId(GeneratorUUID.generateUUID());
		flow.setVcRecordId(vcRecordId);
		flow.setDtDate(new Date());
		flow.setVcSequence(1);//节点序号		
		flow.setVcType(vcType);
		flow.setVcDelFlag("0");
		flow.setVcWorkId(work.getVcId());
		flow.setVcHandleOrgId(vcHandleOrgId);//处理机构
		flow.setVcHandleOrgName(vcHandleOrgName);
		flow.setVcHandlePersonId(vcHandlePersonId);//处理人
		flow.setVcHandlePersonName(vcHandlePersonName);
		flow.setVcStatus("0");//节点未处理		
		
		WorkflowMan man=new WorkflowMan();
		
		man.setVcId(GeneratorUUID.generateUUID());
		man.setDtDate(new Date());
		man.setVcFlowId(flow.getVcId());
		man.setVcRecordId(vcRecordId);
		man.setVcDelFlag("0");
		man.setVcPromanId(User.getUserId());
		man.setVcPromanName(User.getUserName());
		man.setVcOrgId(User.getUserOrgId());
		man.setVcOrgName(User.getUserOrgName());
		man.setVcAdvance(vcDesc);
		man.setVcStatus("1");
		
		try {
			FlowWorkUtil.getBaseLogic().addPo(work);
			FlowWorkUtil.getBaseLogic().addPo(flow);
			FlowWorkUtil.getBaseLogic().addPo(man);
		} catch (AppException e) {
			e.printStackTrace();
		}
		
	}
	
	//案源设为已选时，即关联流程表，并把案源设为待启动。
		public static void submitFlowWork(String vcTableName,String vcRecordId,String vcType,String vcOrgId,String vcOrgName,String vcPersonId,String vcPersonName,String vcHandleOrgId,String vcHandleOrgName,String vcHandlePersonId,String vcHandlePersonName,String vcDesc){
			WorkflowWork work=new WorkflowWork();	
			work=getWorkflowWork(vcRecordId);
			if(work!=null){
				work.setVcStatus("0");//已下发，待启动
				work.setVcType(vcType);
				work.setVcOrgId(vcOrgId);
				work.setVcOrgName(vcOrgName);
				work.setVcPersonId(vcPersonId);
				work.setVcPersonName(vcPersonName);
				
				work.setVcHandlePersonId(vcHandlePersonId);
				work.setVcHandlePersonName(vcHandlePersonName);
				work.setVcHandleOrgid(vcHandleOrgId);
				work.setVcHandleOrgname(vcHandleOrgName);
				work.setVcDesc(vcDesc);
				
				try {
					FlowWorkUtil.getBaseLogic().updatePo(work);
				} catch (AppException e) {
					e.printStackTrace();
				}
			}else{
				work=new WorkflowWork();
				work.setVcId(GeneratorUUID.generateUUID());
				work.setVcRecordId(vcRecordId);
				work.setDtDate(new Date());
				work.setVcStatus("0");//已下发，待启动
				work.setVcType(vcType);
				work.setVcTableName(vcTableName);		
				
				work.setVcOrgId(vcOrgId);
				work.setVcOrgName(vcOrgName);
				work.setVcPersonId(vcPersonId);
				work.setVcPersonName(vcPersonName);
				
				work.setVcHandlePersonId(vcHandlePersonId);
				work.setVcHandlePersonName(vcHandlePersonName);
				work.setVcHandleOrgid(vcHandleOrgId);
				work.setVcHandleOrgname(vcHandleOrgName);
				
				work.setVcDelFlag("0");
				work.setVcDesc(vcDesc);	
				try {
					FlowWorkUtil.getBaseLogic().addPo(work);
				} catch (AppException e) {
					e.printStackTrace();
				}
			}
					
			
			WorkflowMan man=new WorkflowMan();
			
			man.setVcId(GeneratorUUID.generateUUID());
			man.setDtDate(new Date());
			man.setVcFlowId("");
			man.setVcRecordId(vcRecordId);
			man.setVcDelFlag("0");
			man.setVcPromanId(User.getUserId());
			man.setVcPromanName(User.getUserName());
			man.setVcOrgId(User.getUserOrgId());
			man.setVcOrgName(User.getUserOrgName());
			man.setVcAdvance(vcDesc);
			man.setVcStatus("1");
			
			try {
				FlowWorkUtil.getBaseLogic().addPo(man);
			} catch (AppException e) {
				e.printStackTrace();
			}
			
		}
	
	//案源设为已选时，即关联流程表，并把案源设为待启动。
		public static void setDownFlowWork(String vcRecordId,String vcType,String vcOrgId,String vcOrgName,String vcPersonId,String vcPersonName,String vcHandleOrgId,String vcHandleOrgName,String vcHandlePersonId,String vcHandlePersonName,String vcDesc){
			WorkflowWork work=new WorkflowWork();	
			work=getWorkflowWork(vcRecordId);
			if(work==null){
				work=new WorkflowWork();	
				work.setVcId(GeneratorUUID.generateUUID());
				work.setVcRecordId(vcRecordId);
				work.setDtDate(new Date());
				work.setVcStatus("1");//已下发，待启动
				work.setVcType(vcType);
				//work.setVcTableName(vcTableName);					
				work.setVcOrgId(vcOrgId);
				work.setVcOrgName(vcOrgName);
				work.setVcPersonId(vcPersonId);
				work.setVcPersonName(vcPersonName);				
				work.setVcHandlePersonId(vcHandlePersonId);
				work.setVcHandlePersonName(vcHandlePersonName);
				work.setVcHandleOrgid(vcHandleOrgId);
				work.setVcHandleOrgname(vcHandleOrgName);
				work.setVcDelFlag("0");
				work.setVcDesc(vcDesc);
				try {
					FlowWorkUtil.getBaseLogic().addPo(work);
				} catch (AppException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else{
				work.setDtDate(new Date());
				work.setVcStatus("1");//已下发，待启动
				work.setVcType(vcType);
				work.setVcOrgId(vcOrgId);
				work.setVcOrgName(vcOrgName);
				work.setVcPersonId(vcPersonId);
				work.setVcPersonName(vcPersonName);
				work.setVcHandlePersonId(vcHandlePersonId);
				work.setVcHandlePersonName(vcHandlePersonName);
				work.setVcHandleOrgid(vcHandleOrgId);
				work.setVcHandleOrgname(vcHandleOrgName);
				work.setVcDesc(vcDesc);
				try {
					FlowWorkUtil.getBaseLogic().updatePo(work);
				} catch (AppException e) {
					e.printStackTrace();
				}
			}
			
			
			WorkflowFlow flow=new WorkflowFlow();
			
			flow.setVcId(GeneratorUUID.generateUUID());
			flow.setVcRecordId(vcRecordId);
			flow.setDtDate(new Date());
			flow.setVcSequence(1);//节点序号		
			flow.setVcType(vcType);
			flow.setVcDelFlag("0");
			flow.setVcWorkId(work.getVcId());
			flow.setVcHandleOrgId(vcHandleOrgId);//处理机构
			flow.setVcHandleOrgName(vcHandleOrgName);
			flow.setVcHandlePersonId(vcHandlePersonId);//处理人
			flow.setVcHandlePersonName(vcHandlePersonName);
			flow.setVcStatus("0");//节点未处理		
			
			WorkflowMan man=new WorkflowMan();
			
			man.setVcId(GeneratorUUID.generateUUID());
			man.setDtDate(new Date());
			man.setVcFlowId(flow.getVcId());
			man.setVcRecordId(vcRecordId);
			man.setVcDelFlag("0");
			man.setVcPromanId(User.getUserId());
			man.setVcPromanName(User.getUserName());
			man.setVcOrgId(User.getUserOrgId());
			man.setVcOrgName(User.getUserOrgName());
			man.setVcAdvance(vcDesc);
			man.setVcStatus("1");
			
			try {
				
				FlowWorkUtil.getBaseLogic().addPo(flow);
				FlowWorkUtil.getBaseLogic().addPo(man);
			} catch (AppException e) {
				e.printStackTrace();
			}
			
		}
	
	//启动时为选案环节，案源状态为1
	public static void startFlowWork(String vcWorkId){
		WorkflowWork work=null;
		try {
			work=(WorkflowWork) FlowWorkUtil.getBaseLogic().getPOByPk(WorkflowWork.class.getName(), vcWorkId);;
		} catch (AppException e) {
			e.printStackTrace();
		}
		
		if(null!=work){
			work.setVcStatus("1");
		}
		try {
			FlowWorkUtil.getBaseLogic().updatePo(work);
		} catch (AppException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 移交
	 * @param vcWorkId 流程主键
	 * @param vcType 移交类型 1 移交到人，2移交到机构
	 * @param vcSequence 当前节点序号
	 * @param vcStartDate 节点处理开始日期
	 * @param vcEndDate 节点处理结束时间
	 * @param vcHandleOrgId 节点处理人机构id
	 * @param vcHandleOrgName 节点处理人机构名称
	 * @param vcHandlePersonId 节点处理人id
	 * @param vcHandlePersonName 节点处理人名称
	 * @param vcAdvance
	 */
	public static void transferFlowWork(String vcRecordId,String vcType,String curPersonId,String vcStartDate,String vcEndDate,String vcHandleOrgId,String vcHandleOrgName,String vcHandlePersonId,String vcHandlePersonName,String vcAdvance){
		WorkflowWork work=null;
		WorkflowFlow curflow=new WorkflowFlow();
		WorkflowFlow nextflow=new WorkflowFlow();
		WorkflowMan man=new WorkflowMan();
		
		work=getWorkflowWork(vcRecordId);
		
		curflow=getWorkflowFlowByPerson(vcRecordId,curPersonId);
		
		String hql="from WorkflowFlow p where p.vcRecordId='"+vcRecordId+"' and p.vcSequence="+(curflow.getVcSequence()+1);
		List<WorkflowFlow> nextflows=new ArrayList<WorkflowFlow>();
		try {
			nextflows=(List<WorkflowFlow>) FlowWorkUtil.getBaseLogic().searchByHql(hql);
		} catch (AppException e1) {
			e1.printStackTrace();
		}
		boolean b=false;
		if(nextflows.size()==0){
			nextflow.setVcId(GeneratorUUID.generateUUID());
			nextflow.setVcWorkId(work.getVcId());
			nextflow.setVcRecordId(work.getVcRecordId());
			nextflow.setDtDate(new Date());
			nextflow.setVcSequence((curflow.getVcSequence()+1));//节点序号		
			nextflow.setVcType(vcType);
			nextflow.setVcDelFlag("0");
		}else{
			b=true;
			nextflow=nextflows.get(0);			
		}		
		
		nextflow.setVcHandleOrgId(vcHandleOrgId);//处理机构
		nextflow.setVcHandleOrgName(vcHandleOrgName);
		nextflow.setVcHandlePersonId(vcHandlePersonId);//处理人
		nextflow.setVcHandlePersonName(vcHandlePersonName);
		nextflow.setVcStatus("0");//节点未处理		
		if(StringUtil.isNotBlank(vcStartDate)){
			try {
				nextflow.setDtStartDate(DateUtil.parse(vcStartDate, "yyyy-MM-dd"));
				
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		if(StringUtil.isNotBlank(vcEndDate)){
			try {
				nextflow.setDtEndDate(DateUtil.parse(vcEndDate, "yyyy-MM-dd"));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}	
		curflow.setVcStatus("2");//当前节点设为完成状态		
		
		man.setVcId(GeneratorUUID.generateUUID());
		man.setDtDate(new Date());
		man.setVcFlowId(curflow.getVcId());
		man.setVcRecordId(work.getVcRecordId());
		man.setVcDelFlag("0");
		man.setVcPromanId(User.getUserId());
		man.setVcPromanName(User.getUserName());
		man.setVcOrgId(User.getUserOrgId());
		man.setVcOrgName(User.getUserOrgName());
		man.setVcAdvance(vcAdvance);
		man.setVcStatus("1");
		
		
		if(null!=work){
			work.setVcStatus(""+(curflow.getVcSequence()+1));//主流程状态，和流程节点序号保持一致，即为流程所处四环节中的某一环节,移交到下一环节
			work.setVcHandleOrgid(vcHandleOrgId);
			work.setVcHandleOrgname(vcHandleOrgName);
			work.setVcHandlePersonId(vcHandlePersonId);
			work.setVcHandlePersonName(vcHandlePersonName);
			if(StringUtil.isNotBlank(work.getVcHandleOrgids())){//处理过的机构
				work.setVcHandleOrgids(work.getVcHandleOrgids()+",'"+User.getUserOrgId()+"'");
				work.setVcHandleOrgids(work.getVcHandlePersonIds()+",'"+User.getUserId()+"'");
			}else{
				work.setVcHandleOrgids("'"+User.getUserOrgId()+"'");
				work.setVcHandleOrgids("'"+User.getUserId()+"'");
			}
			if(StringUtil.isNotBlank(work.getVcHandlePersonIds())){//处理过的人
				work.setVcHandlePersonIds(work.getVcHandlePersonIds()+",'"+User.getUserId()+"'");
			}else{
				work.setVcHandlePersonIds("'"+User.getUserId()+"'");
			}
		}
		try {
			FlowWorkUtil.getBaseLogic().updatePo(work);
			FlowWorkUtil.getBaseLogic().addPo(man);
		} catch (AppException e) {
			e.printStackTrace();
		}
		
		if(b){//被回退节点的移交
			try {
				FlowWorkUtil.getBaseLogic().updatePo(nextflow);
			} catch (AppException e) {
				e.printStackTrace();
			}
		}else{
			try {
				FlowWorkUtil.getBaseLogic().addPo(nextflow);
			} catch (AppException e) {
				e.printStackTrace();
			}
		}
		
		try {
			FlowWorkUtil.getBaseLogic().updatePo(curflow);
		} catch (AppException e) {
			e.printStackTrace();
		}
		
	}
	/**
	 * 回退,返回值为0，说明没有返回该节点的权限，1 返回成功，2 该节点已执行过退回操作，不能重复执行
	 * @param vcWorkId
	 * @param vcSequence 当前节点序号
	 * @param vcHandleOrgId 处理人机构id
	 * @param vcHandleOrgName 处理人机构名称
	 * @param vcHandlePersonId 处理人id
	 * @param vcHandlePersonName 处理人名称
	 * @param vcAdvance 处理意见
	 */
		public static int backFlowWork(String vcRecordId,String vcHandleOrgId,String vcHandleOrgName,String vcHandlePersonId,String vcHandlePersonName,String vcAdvance){
			WorkflowWork work=null;
			WorkflowFlow curflow=new WorkflowFlow();
			WorkflowFlow preflow=new WorkflowFlow();
			WorkflowMan man=new WorkflowMan();
			int state=0;
			work=(WorkflowWork) FlowWorkUtil.getWorkflowWork(vcRecordId);
					
			curflow=FlowWorkUtil.getWorkflowFlowByPerson(vcRecordId, User.getUserId());
			if(null==curflow){
				state=2;//已回退，不允许重复操作。
				return state;
			}else{
				state=1;//当前节点已被驳回
			}
			
			preflow=getWorkflowFlowByVcSeq(vcRecordId,""+(curflow.getVcSequence()-1));//当前节点的前一节点
			if(preflow!=null){
				work.setVcHandleOrgid(preflow.getVcHandleOrgId());
				work.setVcHandleOrgname(preflow.getVcHandleOrgName());
				work.setVcHandlePersonId(preflow.getVcHandlePersonId());
				work.setVcHandlePersonName(preflow.getVcHandlePersonName());
			}else{
				state=2;//综合科不允许回退。
			}
			
			//work.setVcStatus(preflow.getVcSequence().toString());
			work.setVcHandleInfo(vcAdvance);
			
			man.setVcId(GeneratorUUID.generateUUID());
			man.setDtDate(new Date());
			man.setVcFlowId(curflow.getVcId());
			man.setVcRecordId(work.getVcRecordId());
			man.setVcDelFlag("0");
			man.setVcPromanId(User.getUserId());
			man.setVcPromanName(User.getUserName());
			man.setVcOrgId(User.getUserOrgId());
			man.setVcOrgName(User.getUserOrgName());
			man.setVcAdvance(vcAdvance);
			man.setVcStatus("-1");		
			List<WorkflowFlow> l=new ArrayList<WorkflowFlow>();
			l.add(curflow);
			try {
				FlowWorkUtil.getBaseLogic().deletePos(l);
			} catch (AppException e1) {
				e1.printStackTrace();
			}
			
			
//			if("-1".equals(curflow.getVcStatus())){
//				state=2;//当前节点已执行过退回操作
//			}else{
//				curflow.setVcStatus("-1");//当前节点已执行退回操作
//				state=1;//当前节点已被驳回
//			}
			
			
			
			if(1==state){
				try {
					if(null!=preflow){//前一节点不为空
						preflow.setVcStatus("1");//回退至已签收在处理状态
						FlowWorkUtil.getBaseLogic().updatePo(preflow);
					}
					if(Integer.parseInt(work.getVcStatus())>1){
						work.setVcStatus(""+(Integer.parseInt(work.getVcStatus())-1));//主流程状态，和流程节点序号保持一致，即为流程所处四环节中的某一环节，回退到上一环节
						FlowWorkUtil.getBaseLogic().updatePo(work);
					}	
					
					//删掉回退节点生成的文书
					FlowWorkUtil.getBaseLogic().deleteSearchHQL(" From CaseSourceFiles p where p.vcFlowId='"+curflow.getVcId()+"'");
					
					FlowWorkUtil.getBaseLogic().addPo(man);
				} catch (AppException e) {
					e.printStackTrace();
				}
			}
			
			return state;//驳回操作成功
		}
		/**
		 * 审核
		 * @param vcWorkId
		 * @param vcSequence
		 * @param vcHandleOrgId
		 * @param vcHandleOrgName
		 * @param vcHandlePersonId
		 * @param vcHandlePersonName
		 * @param vcAdvance
		 */
		public static void updateFlowWork(String vcRecordId,String vcHandleOrgId,String vcHandleOrgName,String vcHandlePersonId,String vcHandlePersonName,String vcAdvance){
			//String hql="from WorkflowFlow p where p.vcWorkId='"+vcWorkId+"' and p.vcStatus='0'";//当前节点
			WorkflowWork work=null;
			work=FlowWorkUtil.getWorkflowWork(vcRecordId);
			work.setVcHandlePersonId(vcHandlePersonId);
			work.setVcHandlePersonName(vcHandlePersonName);
			work.setVcHandleOrgid(vcHandleOrgId);
			work.setVcHandleOrgname(vcHandleOrgName);	
			work.setVcHandleInfo(vcAdvance);
			
			WorkflowMan man=new WorkflowMan();		
			
			man.setVcId(GeneratorUUID.generateUUID());
			man.setDtDate(new Date());
			man.setVcFlowId("");
			man.setVcRecordId(vcRecordId);
			man.setVcDelFlag("0");
			man.setVcPromanId(User.getUserId());
			man.setVcPromanName(User.getUserName());
			man.setVcOrgId(User.getUserOrgId());
			man.setVcOrgName(User.getUserOrgName());
			man.setVcAdvance(vcAdvance);
			man.setVcStatus("1");			
			try {
				FlowWorkUtil.getBaseLogic().updatePo(work);
				FlowWorkUtil.getBaseLogic().addPo(man);
			} catch (AppException e) {
				e.printStackTrace();
			}
		}
		public static void receiveFlowWork(String vcWorkId,int vcSequence,String vcHandleOrgId,String vcHandleOrgName,String vcHandlePersonId,String vcHandlePersonName){
			
		}
		/**
		 * 获取待办案源
		 * @param curOrgId
		 * @param curUserId
		 * @return
		 */
		public static List<CaseSource> getCaseSources(String curOrgId,String curUserId){
			List<CaseSource> caseSources=new ArrayList<CaseSource>();
			String sql="select t1.* from CASE_SOURCE t1,T_WORKFLOW_WORK t2,T_WORKFLOW_FLOW t3  where t1.vc_id=t2.VC_RECORD_ID and t2.vc_id=t3.VC_WORK_ID and t2.VC_TABLE_NAME='CASE_SOURCE' and t1.vc_del_flag='0' and t3.VC_STATUS='0'";
			sql+=" and ((t2.vc_type='1' and t2.VC_HANDLE_PERSON_ID like '%"+curUserId+"%') or (t2.vc_type='2' and t2.VC_HANDLE_ORG_ID = '"+curOrgId+"'))";//转给本机构或者本人的
			
			FlowWorkUtil.getBaseLogic().executeSQLQuery(sql, new ISQLMapper(){
				@Override
				public Object fromSQLQuery(ResultSet rs, int num) {
					CaseSource po=new CaseSource();
					try {
						po.setVcId(rs.getString("vc_id"));
						po.setCaseFromNum(rs.getString("CASE_FROM_NUM"));//案源编号
						po.setCaseFromName(rs.getString("CASE_FROM_NAME"));//案件来源
						po.setTaxpayerNumber(rs.getString("TAXPAYER_NUMBER"));//纳税人识别号
						po.setInvolvedEnterprice(rs.getString("INVOLVED_ENTERPRICE"));//企业名称
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					return po;
				}
				
			});
			return caseSources;
		}
		
		/**
		 * 获取待办案源
		 * @param curOrgId
		 * @param curUserId
		 * @return
		 */
		public static List<CaseSource> getCaseSourceDb(String curOrgId,String curUserId){
			List<CaseSource> caseSources=new ArrayList<CaseSource>();
			String sql="select t1.*,t2.VC_HANDLE_INFO from CASE_SOURCE t1,T_WORKFLOW_WORK t2  where t1.vc_id=t2.VC_RECORD_ID and t2.VC_TABLE_NAME='CASE_SOURCE' and t1.vc_del$flag='0'";
			sql+=" and ((t2.vc_type='1' and t2.VC_HANDLE_PERSON_ID like '%"+curUserId+"%') or (t2.vc_type='2' and t2.vc_handle_orgid = '"+curOrgId+"'))";//转给本机构或者本人的
			//sql="select t1.* from CASE_SOURCE t1";
			caseSources=(List<CaseSource>) FlowWorkUtil.getBaseLogic().executeSQLQuery(sql, new ISQLMapper(){
				@Override
				public Object fromSQLQuery(ResultSet rs, int num) {
					CaseSource po=new CaseSource();
					try {
						po.setVcId(rs.getString("vc_id"));
						po.setCaseFromNum(rs.getString("CASE_FROM_NUM"));//案源编号
						po.setCaseFromName(rs.getString("CASE_FROM_NAME"));//案件来源
						po.setTaxpayerNumber(rs.getString("TAXPAYER_NUMBER"));//纳税人识别号
						po.setInvolvedEnterprice(rs.getString("INVOLVED_ENTERPRICE"));//企业名称
						po.setStatusInfo(rs.getString("VC_HANDLE_INFO"));
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					return po;
				}
				
			});
			return caseSources;
		}
		
		@SuppressWarnings("unchecked")
		public static List<WorkflowMan> getWorkflowMansByRecordId(String vcRecordId){
			List<WorkflowMan> mans=new ArrayList<WorkflowMan>();
			try {
				mans=(List<WorkflowMan>) FlowWorkUtil.getBaseLogic().searchByHql("from WorkflowMan p where p.vcRecordId='"+vcRecordId+"' order by p.dtDate");
			} catch (AppException e) {
				e.printStackTrace();
			}
			
			return mans;
		}
		public static WorkflowWork getWorkflowWork(String vcRecordId){
			String hql="from WorkflowWork p where p.vcRecordId='"+vcRecordId+"' and p.vcDelFlag='0'";
			List<WorkflowWork> works=new ArrayList<WorkflowWork>();
			try {
				works = (List<WorkflowWork>) FlowWorkUtil.getBaseLogic().searchByHql(hql);
			} catch (AppException e) {
				e.printStackTrace();
			}
			if(null!=works&&works.size()>0){
				return works.get(0);
			}
			return null;
		}
		/*当前人待处理的节点*/
		public static WorkflowFlow getWorkflowFlowStatuIs0ByPerson(String vcRecordId,String curPersonId){
			String hql="from WorkflowFlow p where p.vcRecordId='"+vcRecordId+"' and p.vcHandlePersonId='"+curPersonId+"' and p.vcStatus='0' and p.vcDelFlag='0'";
			List<WorkflowFlow> works=new ArrayList<WorkflowFlow>();
			try {
				works = (List<WorkflowFlow>) FlowWorkUtil.getBaseLogic().searchByHql(hql);
			} catch (AppException e) {
				e.printStackTrace();
			}
			if(null!=works&&works.size()>0){
				return works.get(0);
			}
			return null;
		}
		
		public static WorkflowFlow getWorkflowFlowByPerson(String vcRecordId,String curPersonId){
			String hql="from WorkflowFlow p where p.vcRecordId='"+vcRecordId+"' and p.vcHandlePersonId='"+curPersonId+"' and p.vcDelFlag='0' order by p.vcSequence desc";
			List<WorkflowFlow> works=new ArrayList<WorkflowFlow>();
			try {
				works = (List<WorkflowFlow>) FlowWorkUtil.getBaseLogic().searchByHql(hql);
			} catch (AppException e) {
				e.printStackTrace();
			}
			if(null!=works&&works.size()>0){
				return works.get(0);
			}
			return null;
		}
		/**
		 * 当前人正在处理的节点
		 * @param vcRecordId
		 * @param curPersonId
		 * @return
		 */
		public static WorkflowFlow getWorkflowFlowByPersonCling(String vcRecordId,String curPersonId){
			String hql="from WorkflowFlow p where p.vcRecordId='"+vcRecordId+"' and p.vcHandlePersonId='"+curPersonId+"' and (p.vcStatus='0' or p.vcStatus='1')  and p.vcDelFlag='0' order by p.vcSequence desc";
			List<WorkflowFlow> works=new ArrayList<WorkflowFlow>();
			try {
				works = (List<WorkflowFlow>) FlowWorkUtil.getBaseLogic().searchByHql(hql);
			} catch (AppException e) {
				e.printStackTrace();
			}
			if(null!=works&&works.size()>0){
				return works.get(0);
			}
			return null;
		}
		
		public static WorkflowFlow getWorkflowFlowByOrgId(String vcRecordId,String curOrgId){
			String hql="from WorkflowFlow p where p.vcRecordId='"+vcRecordId+"' and p.vcHandleOrgId='"+curOrgId+"' and p.vcDelFlag='0' order by p.vcSequence desc";
			List<WorkflowFlow> works=new ArrayList<WorkflowFlow>();
			try {
				works = (List<WorkflowFlow>) FlowWorkUtil.getBaseLogic().searchByHql(hql);
			} catch (AppException e) {
				e.printStackTrace();
			}
			if(null!=works&&works.size()>0){
				return works.get(0);
			}
			return null;
		}
		/**
		 * 返回值为真，则签收。
		 * @param vcRecordId
		 * @param vcHandleOrgId
		 * @return
		 */
		public static int setReceiveFlag(String vcRecordId,String vcHandleOrgId,String vcHandleOrgName,String vcHandlePersonId,String vcHandlePersonName){
			String hql="from WorkflowFlow p where p.vcRecordId='"+vcRecordId+"' and p.vcHandleOrgId='"+User.getUserOrgId()+"'  and p.vcStatus='0' and p.vcDelFlag='0'";//待签收节点
			List<WorkflowFlow> flows=new ArrayList<WorkflowFlow>();
			try {
				flows = (List<WorkflowFlow>) FlowWorkUtil.getBaseLogic().searchByHql(hql);
			} catch (AppException e) {
				e.printStackTrace();
			}
			int state=0;
			if(null!=flows&&flows.size()>0){
				
				WorkflowFlow flow=flows.get(0);

				flow.setVcStatus("1");//签收为处理中
				flow.setVcHandleOrgName(vcHandleOrgName);
				flow.setVcHandleOrgId(vcHandleOrgId);
				flow.setVcHandlePersonId(vcHandlePersonId);
				flow.setVcHandlePersonName(vcHandlePersonName);
				flow.setVcDesc("已签收");
				WorkflowWork work=FlowWorkUtil.getWorkflowWork(vcRecordId);
				work.setVcHandleInfo("已签收");
				work.setVcStatus(""+flow.getVcSequence());
				WorkflowMan man=new WorkflowMan();
				man.setVcId(GeneratorUUID.generateUUID());
				man.setDtDate(new Date());
				man.setVcFlowId(flow.getVcId());
				man.setVcRecordId(flow.getVcRecordId());
				man.setVcDelFlag("0");
				man.setVcPromanId(User.getUserId());
				man.setVcPromanName(User.getUserName());
				man.setVcOrgId(User.getUserOrgId());
				man.setVcOrgName(User.getUserOrgName());
				man.setVcAdvance("已签收");
				man.setVcStatus("1");			
				try {
					FlowWorkUtil.getBaseLogic().updatePo(flow);
					FlowWorkUtil.getBaseLogic().addPo(man);
					FlowWorkUtil.getBaseLogic().updatePo(work);
				} catch (AppException e) {
					e.printStackTrace();
				}
				state=1;
			
			}
			return state;
		}
		/**
		 * 获取vcHandleOrgId的vcRecordId的当前节点
		 * @param vcRecordId
		 * @param vcHandleOrgId
		 * @return
		 */
		public static WorkflowFlow getWorkflowFlow(String vcRecordId,String vcStatus){
			String hql="from WorkflowFlow p where p.vcRecordId='"+vcRecordId+"' and  p.vcStatus='"+vcStatus+"' and p.vcDelFlag='0'";//待签收节点
			List<WorkflowFlow> flows=new ArrayList<WorkflowFlow>();
			try {
				flows = (List<WorkflowFlow>) FlowWorkUtil.getBaseLogic().searchByHql(hql);
			} catch (AppException e) {
				e.printStackTrace();
			}
			int state=0;
			if(null!=flows&&flows.size()>0){
				return flows.get(0);
				
			}
			return null;
		}
		/**
		 * 获取vcRecordId的所有节点
		 * @param vcRecordId
		 * @param vcHandleOrgId
		 * @return
		 */
		public static List<WorkflowFlow> getWorkflowFlows(String vcRecordId){
			String hql="from WorkflowFlow p where p.vcRecordId='"+vcRecordId+"' and p.vcDelFlag='0' order by p.vcSequence";//待签收节点
			List<WorkflowFlow> flows=new ArrayList<WorkflowFlow>();
			try {
				flows = (List<WorkflowFlow>) FlowWorkUtil.getBaseLogic().searchByHql(hql);
			} catch (AppException e) {
				e.printStackTrace();
			}
			return flows;
		}
		/**
		 * 获取vcHandleOrgId的vcRecordId的当前节点
		 * @param vcRecordId
		 * @param vcHandleOrgId
		 * @return
		 */
		public static WorkflowFlow getCurWorkflowFlow(String vcRecordId){
			String hql="from WorkflowFlow p where p.vcRecordId='"+vcRecordId+"' and (p.vcStatus='0' or p.vcStatus='3')";//待签收节点
			List<WorkflowFlow> flows=new ArrayList<WorkflowFlow>();
			try {
				flows = (List<WorkflowFlow>) FlowWorkUtil.getBaseLogic().searchByHql(hql);
			} catch (AppException e) {
				e.printStackTrace();
			}
			int state=0;
			if(null!=flows&&flows.size()>0){
				return flows.get(0);
				
			}
			return null;
		}
		/**
		 * 获取vcHandleOrgId的vcRecordId的当前节点
		 * @param vcRecordId
		 * @param vcHandleOrgId
		 * @return
		 */
		public static WorkflowFlow getCurOrgWorkflowFlow(String vcRecordId){
			String hql="from WorkflowFlow p where p.vcRecordId='"+vcRecordId+"' and  p.vcHandleOrgId='"+User.getUserOrgId()+"'";//待签收节点
			List<WorkflowFlow> flows=new ArrayList<WorkflowFlow>();
			try {
				flows = (List<WorkflowFlow>) FlowWorkUtil.getBaseLogic().searchByHql(hql);
			} catch (AppException e) {
				e.printStackTrace();
			}
			int state=0;
			if(null!=flows&&flows.size()>0){
				return flows.get(0);
				
			}
			return null;
		}
		
		/**
		 * 获取vcSequence的vcRecordId的当前节点
		 * @param vcRecordId
		 * @param vcSequence
		 * @return
		 */
		public static WorkflowFlow getWorkflowFlowByVcSeq(String vcRecordId,String vcSequence){
			String hql="from WorkflowFlow p where p.vcRecordId='"+vcRecordId+"' and  p.vcSequence='"+vcSequence+"'";//待签收节点
			List<WorkflowFlow> flows=new ArrayList<WorkflowFlow>();
			try {
				flows = (List<WorkflowFlow>) FlowWorkUtil.getBaseLogic().searchByHql(hql);
			} catch (AppException e) {
				e.printStackTrace();
			}
			int state=0;
			if(null!=flows&&flows.size()>0){
				return flows.get(0);
				
			}
			return null;
		}
		/**
		 * 功能描述：节点处理人（实例）
		 * @param vcRecordId
		 * @param vcAdvice  操作描述
		 * @return
		 */
		public static void saveWorkflowMan(String vcRecordId,String vcAdvice){
			String currOrgId=User.getUserOrgId();
			String hql="from WorkflowFlow p where p.vcRecordId='"+vcRecordId+"' and p.vcHandleOrgId='"+currOrgId+"'";//待签收节点
			List<WorkflowFlow> flows=new ArrayList<WorkflowFlow>();
			try {
				flows = (List<WorkflowFlow>) FlowWorkUtil.getBaseLogic().searchByHql(hql);
			} catch (AppException e) {
				e.printStackTrace();
			}
			WorkflowFlow flow=null;
			if(flows!=null&&flows.size()>0){
				flow=flows.get(0);
			}
			WorkflowMan man=new WorkflowMan();
			man.setVcId(GeneratorUUID.generateUUID());
			man.setDtDate(new Date());
			if(flow!=null)
				man.setVcFlowId(flow.getVcId());
			man.setVcRecordId(vcRecordId);
			man.setVcDelFlag("0");
			man.setVcPromanId(User.getUserId());
			man.setVcPromanName(User.getUserName());
			man.setVcOrgId(User.getUserOrgId());
			man.setVcOrgName(User.getUserOrgName());
			man.setVcAdvance(vcAdvice);
			man.setVcStatus("1");				
			try {
				FlowWorkUtil.getBaseLogic().addPo(man);
			} catch (AppException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		public static List<CaseSourceFilesTemplate> getWorkflowTemplateFiles(String vcSequence,String vcBrowse){
			List<CaseSourceFilesTemplate> l=new ArrayList<CaseSourceFilesTemplate>();
			String sql="from CaseSourceFilesTemplate t where t.vcDelFlag=0 and t.vcSequence='"+vcSequence+"'";
			if(StringUtil.isNotBlank(vcBrowse)){
				sql+="  and t.vcBrowse='"+vcBrowse+"'";
			}
			l=(List<CaseSourceFilesTemplate>) DbUtil.executeHql(sql);
			return l;
		}
}
